package com.ytxg.health_management.article.controller;

import com.ytxg.health_management.article.pojo.ArticleMain;
import com.ytxg.health_management.controller.UserController;
import com.ytxg.health_management.likeRecords.entity.LikeRecords;
import com.ytxg.health_management.pojo.dto.ArticlePageDto;
import com.ytxg.health_management.pojo.dto.PageArticle;
import com.ytxg.health_management.pojo.dto.UserDto;
import com.ytxg.health_management.pojo.entity.*;
import com.ytxg.health_management.pojo.vo.ArticleVo;
import com.ytxg.health_management.article.service.ArticleService;
import com.ytxg.health_management.likeRecords.service.LikeRecordsService;
import com.ytxg.health_management.util.ResultCode;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @Resource
    ArticleService articleService;
    @Resource
    UserController userController;
    @Resource
    LikeRecordsService likeRecordsService;

    /**
     * 获取文章列表
     */
    @PostMapping("/list")
    public Result<Map<String, Object>> getArticleList(@RequestBody Page page, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return articleService.getListByCreateName(userDtoResult.getData().getUserName(), page);
    }

    /**
     * 搜索文章
     */
    @GetMapping("/search")
    public Result<List<ArticleMain>> getSearch(@RequestParam("name") String name) {
        log.info("有人搜索文章，文章标题为: {}", name);
        return articleService.searchCommon(new ArticleMain().setTitle(name));
    }

    /**
     * 新增文章
     */
    @PostMapping("/add")
    public Result<String> uploadArticle(@RequestBody ArticleVo data, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        if (userDtoResult.getCode() == ResultCode.USER_AUTH_FALL) {
            return Result.fail("用户未登录或信息已过期");
        }

        if (data.getId() != null) {
            return articleService.updateArticle(data);
        } else {
            return articleService.addArticle(data, userDtoResult.getData());

        }

    }

    /**
     * 删除文章
     * 没有考虑删除人是否是文章作者 TODO
     */
    @PostMapping("/delete")
    public Result<String> deleteArticle(@RequestBody List<Integer> id, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        if (userDtoResult.getCode() == ResultCode.USER_AUTH_FALL) {
            return Result.fail("用户未登录或信息已过期");
        }

        return articleService.deleteById(id);
    }


    @PostMapping("/listPage")
    public Result<PageArticle> getArticleList(@RequestBody ArticlePageDto articlePageDto) {

        return articleService.getPages(
                new ArticleMain().setArticleType(articlePageDto.getArticleType())
                        .setIsVip(articlePageDto.getIsVip())
                        .setStatus(articlePageDto.getStatus())
                        .setTitle(articlePageDto.getTitle()),
                new Page().setPageSize(articlePageDto.getPageSize())
                        .setCurrentPage(articlePageDto.getCurrentPage()));
    }


    /**
     * 文章详情页查询
     */
    @GetMapping("/getById")
    public Result<ArticleMain> getArticleById(@RequestParam(name = "id") int id) {
        //根据id获取文章
        return articleService.getArticleById(id);
    }

    /**
     * 文章收藏
     */
    @PostMapping("/subscribe")
    public Result<String> subscribeArticle(@RequestBody Subscribe subscribe, @RequestHeader("Authorization") String token) {
        userController.tokenAuth(token);
        return articleService.addSubscribe(subscribe);
    }

    /**
     * 文章点赞
     */
    @PostMapping("/like")
    public Result<String> likeArticle(@RequestBody LikeRecords likeRecords, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);

        return likeRecordsService.addLike(likeRecords, userDtoResult.getData());
    }

    /**
     * 获取收藏文章
     */
    @GetMapping("/getSubscribeArticle")
    public Result<List<Subscribe>> geSubscribeArticle(@RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return articleService.getSubscribeArticle(userDtoResult.getData().getId());
    }

    /**
     * 删除收藏文章
     */
    @GetMapping("/deleteSubscribeArticle")
    public Result<String> geSubscribeArticle(@RequestParam("id") int id, @RequestParam("articleId") int articleId, @RequestHeader("Authorization") String token) {
        userController.tokenAuth(token);
        return articleService.deleteSubscribe(id, articleId);
    }

    /**
     * 文章封禁
     */
    @GetMapping("/articleBan")
    public Result<String> articleBan(@RequestParam("articleId") int articleId, @RequestParam("status") String status, @RequestHeader("Authorization") String token) {
        userController.tokenAuth(token);
        return articleService.articleBan(articleId, status);
    }

    @PostMapping("/listAll")
    public Result<HashMap<String, Object>> getArticleList(@RequestBody ArticleMain articleMain) {
        return articleService.getArticleAll(articleMain);
    }
    @PostMapping("/toTop")
    public Result<String> articleToTop(@RequestBody ArticleMain articleMain,@RequestHeader("Authorization") String token) {
        userController.tokenAuth(token);
        return articleService.toTop(articleMain);
    }
}
